package com.ebt.m.policy.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.data.rxModel.api.PolicyAPI;
import com.ebt.m.data.rxModel.apibean.ProductItemsBean;
import com.ebt.m.policy.bean.EventFinishAct;
import com.ebt.m.data.rxModel.apibean.PolicyProposalItem;
import com.ebt.m.data.rxModel.apibean.PolicyUrl;
import com.ebt.m.policy.presenter.PolicyCustomerProposalListPresenter;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by andy on 17-5-31.
 */

public class PolicyCustomerProposalListView extends BaseListView<PolicyCustomerProposalListPresenter> {
    EBTProgressDialog mProgressDialog;

    public PolicyCustomerProposalListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PolicyCustomerProposalListView(Context context) {
        this(context, null);
    }


    public PolicyCustomerProposalListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        mRecyclerView.addItemDecoration(new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, (int) getResources().getDimension(R.dimen.common_divider_dimen), getResources().getColor(R.color.common_divider_color)));
        mRefreshLayout.setRefreshNeeded(false);
        initProgressDialog();
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            switch (view.getId()) {
                case R.id.tv_import:
                    importPolicy(args);
                    break;
                case R.id.ll_content:
                    toProposalDetail(args);
                    break;
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }

        private void importPolicy(Object[] args) {
            showProgress(true);
            PolicyProposalItem pp = (PolicyProposalItem) args[0];
            PolicyAPI opportunityApi = AppInitService.getPolicyApi();
            opportunityApi.getPolicyUrl(pp.pickNum)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(result -> {
                        showProgress(false);
                        PolicyUrl url = result.getData();
                        toPolicy(url.url);
                    }, throwable -> {
                        throwable.printStackTrace();
                        showProgress(false);
                        throwable.printStackTrace();
                        NetUtils.checkHttpException(getContext(), throwable);
                    });
        }

        private void toPolicy(String url) {
            if (!TextUtils.isEmpty(url)) {
                Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "在线投单");
                bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
                bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, true);
                bundle.putString(WebBaseActivity.WEB_TYPE_LINK, url);
                webIntent.putExtras(bundle);
                getContext().startActivity(webIntent);
                EventBus.getDefault().post(new EventFinishAct());
            }
        }

        private void toProposalDetail(Object[] args) {
            PolicyProposalItem pp = (PolicyProposalItem) args[0];
            String applicantName = (String) args[1];
            ProposalListItem p = new ProposalListItem();
            p.status = pp.proposalStatus;
            p.pickNum = pp.pickNum;
            p.applicantName = applicantName;
            p.proposalName = pp.name;//name:建议书名称就是主险名称
            p.previewUrl = pp.previewUrl;
            p.viewUrl = pp.viewUrl;
            ProposalUtils.gotoItemFromProposalList(getContext(), p);
        }
    };

    @Override
    protected ISectionFactory createViewFactory() {
        return new ISectionFactory() {
            @Override
            public IView createView(int viewType) {
                switch (viewType) {
                    default:
                        PolicyProposalItemView itemView = new PolicyProposalItemView(getContext());
                        itemView.setOnClickViewListener(onClickListner);
                        return itemView;
                }
            }
        };
    }

    @Override
    protected PolicyCustomerProposalListPresenter createPresenter() {
        return new PolicyCustomerProposalListPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {
    }

    @Override
    public void update(Object... args) {
        super.update(args);
        List<ProductItemsBean> list = (List<ProductItemsBean>) args[0];
        mPresenter.loadNew(list);
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.layout_product_search_empty, null);
        ImageView imageView = (ImageView) emptyContent.findViewById(R.id.img_empty);
        TextView textView = (TextView) emptyContent.findViewById(R.id.mession_empty);
        imageView.setBackgroundResource(R.drawable.ic_empty_product);
        textView.setText("没有相关计划书");
        return emptyContent;
    }

    public void initProgressDialog() {
        mProgressDialog = new EBTProgressDialog(getContext());
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }

    public void showProgress(boolean show) {
        if (show) {
            mProgressDialog.show();
        } else {
            mProgressDialog.dismiss();
        }
    }
}
